package Test;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.*;

public class Test_03 extends JFrame implements ActionListener,ItemListener{
    //用户的信息
    String Name,Pass,Sex,Birth,Hobit,Other;
    //框架内部件
    JLabel Account,Passw,Passw_C,sex,birth,years,month,day,hobit,other,Tips;
    JButton Go_in,clean;
    JTextField Account_in;
    JPasswordField Passw_in,Passw_C_in;
    JPanel LeftP,RightP,LowP,Psex,Pbirth,Phobit,Pother;
    //定义初始值
    JRadioButton boy = new JRadioButton("男");
    JRadioButton girl = new JRadioButton("女");
    JTextArea JTother = new JTextArea(4,20);
    JComboBox JCyear,JCmonth,JCday;
    String[] Ho = {"篮球","唱歌","跳舞","绘画","看书","围棋","游泳","足球","网球","跑步","游戏","其他"};
    JCheckBox[] jcb = new JCheckBox[12];
    //构造方法
    public Test_03(String title) {
        super(title);
        setSize(550,350);	//显示框大小
        Account = new JLabel("    账    号*:    ");
        Passw = new JLabel( "    密    码*:    ");
        Passw_C = new JLabel("    确认密码*:");
        sex = new JLabel("    性    别*:    ");
        birth = new JLabel("生日*:");
        years = new JLabel("年 ");
        month = new JLabel("月 ");
        day = new JLabel("日");
        hobit = new JLabel("  兴趣:  ");
        other = new JLabel("备注:   ");
        Tips = new JLabel("",JLabel.CENTER);
        Go_in = new JButton("注册");
        clean = new JButton("重置");
        //注册监听器
        Go_in.addActionListener(this);
        clean.addActionListener(this);

        Account_in = new JTextField(15);
        Passw_in = new JPasswordField(15);
        Passw_C_in = new JPasswordField(15);

        LeftP = new JPanel();
        RightP = new JPanel();
        LowP = new JPanel();
        Psex = new JPanel();
        Pbirth = new JPanel();
        Phobit = new JPanel();
        Pother = new JPanel();
        //开始布局
        //左边布局
        LeftP.setLayout(new FlowLayout(FlowLayout.LEFT));
        LeftP.setPreferredSize(new Dimension(240,20));
        LeftP.add(Account);	LeftP.add(Account_in);
        LeftP.add(Passw);	LeftP.add(Passw_in);
        LeftP.add(Passw_C);	LeftP.add(Passw_C_in);

        //性别框
        ButtonGroup sexGroup = new ButtonGroup();
        sexGroup.add(boy);	sexGroup.add(girl);
        Psex.setLayout(new GridLayout(2,1,2,2));
        Psex.setBorder(new BevelBorder(BevelBorder.LOWERED));
        Psex.add(boy);	Psex.add(girl);
        LeftP.add(sex);LeftP.add(Psex);
        //注册监听器
        boy.addItemListener(this);
        girl.addItemListener(this);

        //右边布局
        RightP.setLayout(new FlowLayout(FlowLayout.RIGHT));
        RightP.setPreferredSize(new Dimension(240,20));

        String[] Year = new String[22];	//年
        for(int k=1;k<22;k++) {
            Year[k] = ""+(1989+k);	}
        JCyear = new JComboBox(Year);
        String[] Month = new String[13];	//月
        for(int j=1;j<13;j++) {
            Month[j] = ""+j;	}
        JCmonth = new JComboBox(Month);
        String[] Day = new String[31];	//日
        for(int i=1;i<31;i++) {
            Day[i] = ""+i;	}
        JCday = new JComboBox(Day);
        //生日框
        Pbirth.add(JCyear);		Pbirth.add(years);
        Pbirth.add(JCmonth);	Pbirth.add(month);
        Pbirth.add(JCday);		Pbirth.add(day);
        RightP.add(birth);	RightP.add(Pbirth);
        //兴趣框
        Phobit.setLayout(new GridLayout(3,4,4,4));
        for(int i=0;i<12;i++) {
            jcb[i] =new JCheckBox(Ho[i]);
            Phobit.add(jcb[i]);
        }
        Phobit.setBorder(new BevelBorder(BevelBorder.LOWERED));
        RightP.add(hobit);	RightP.add(Phobit);
        //备注框
        Pother.setLayout(new FlowLayout());
        Pother.add(JTother);
        RightP.add(other,"West");
        RightP.add(Pother);
        //底边布局
        LowP.setLayout(new BorderLayout());
        LowP.setPreferredSize(new Dimension(0,65));
        JPanel p1 = new JPanel();
        p1.add(Go_in);	p1.add(clean);
        LowP.add(p1);
        LowP.add(Tips,"South");
        //合并汇总
        setLayout(new BorderLayout());
        add(LeftP,"West");add(RightP);
        add(LowP,"South");
    }
    public void itemStateChanged(ItemEvent e) {
        //收集性别信息
        if(e.getItemSelectable().equals(boy)){
            Sex = "男";	}
        else if(e.getItemSelectable().equals(girl)) {
            Sex = "女";	}
    }
    //按钮监听器
    public void actionPerformed(ActionEvent e){

        try {
            if(e.getSource()==Go_in) {	//事件触发
                Name = Account_in.getText();
                Birth = JCyear.getSelectedItem().toString()+"年"+JCmonth.getSelectedItem()+"月"+JCday.getSelectedItem()+"日";
                String Ho1 = "";
                for(int i=0;i<12;i++) {
                    if(jcb[i].isSelected()) {
                        Ho1 += Ho[i]+",";	}
                }
                if(Ho1.length()>0) {
                    Hobit = Ho1.substring(0,Ho1.length()-1); }
                else { Hobit = "无"; }
                if(JTother.getText().equals("")) {
                    Other = "无";	}
                else { Other = JTother.getText(); }

                if(new String(Passw_in.getPassword()).equals(new String(Passw_C_in.getPassword()))){
                    Tips.setText("注册成功");

                    System.out.println("用户名(账号):"+Name);
                    System.out.println("性别:"+Sex);
                    System.out.println("生日:"+Birth);
                    System.out.println("兴趣:"+Hobit);
                    System.out.println("备注:"+Other);
                    System.out.println();
                }
                else {	//密码不一致反馈
                    Tips.setText("**输入的两次密码不一致，请重新设置**");
                    Passw_C_in.setText("");
                }
            }
            else {
                Tips.setText("");
                Account_in.setText("");
                Passw_in.setText("");
                Passw_C_in.setText("");
            }
        }
        catch(Exception p) {
            Tips.setText("**信息不全，请补全信息**");
        }
    }
    //主函数入口
    public static void main(String[] args) {
        Test_03 F = new Test_03("注册账号");
        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		F.setResizable(false);
        F.setLocationRelativeTo(null);
        F.setVisible(true);
    }
}

